package com.durian.base.update

import android.content.Context
import androidx.annotation.DrawableRes
import com.durian.base.update.proxy.*
import com.durian.base.update.proxy.impl.*

/**
 * 说明：XUpdate
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2021/1/22 22:27
 * <p/>
 * 版本：version 1.0
 */

fun XUpdate.request() : IUpdateRequest{
    return XUpdateRequest()
}

object XUpdate {

    private lateinit var _context: Context
    private lateinit var _httpRequest: IHttpRequest
    private lateinit var _updateChecker: IUpdateChecker
    private lateinit var _updateListener: IUpdateListener
    private lateinit var _updateConfig: IUpdateConfig
    private lateinit var _updateDownloader: IUpdateDownloader
    private lateinit var _updateInstaller: IUpdateInstaller
    private var smallIcon = 0
    private var _debug = false
    private var _isDownloading = false
    private val _log by lazy { XUpdateLog("XUpdateLog", _debug) }

    fun init(context: Context?) {
        if (context == null || this::_context.isInitialized) {
            return
        }
        _context = context.applicationContext
    }

    fun getContext(): Context {
        return _context
    }

    fun getSmallIcon(): Int {
        return smallIcon
    }

    fun getLog(): XUpdateLog {
        return _log
    }

    fun setDebug(debug: Boolean): XUpdate{
        _debug = debug
        return this
    }

    fun setSmallIcon(@DrawableRes icon: Int): XUpdate {
        smallIcon = icon
        return this
    }

    fun setHttpRequest(httpRequest: IHttpRequest): XUpdate {
        if (!isInitHttpRequest()) {
            this._httpRequest = httpRequest
        }
        return this
    }

    private fun isInitHttpRequest(): Boolean {
        return this::_httpRequest.isInitialized
    }

    fun getHttpRequest(): IHttpRequest {
        if (!isInitHttpRequest()) {
            setHttpRequest(AbsHttpRequest())
        }
        return _httpRequest
    }

    fun setUpdateChecker(updateChecker: IUpdateChecker): XUpdate {
        if (!isInitUpdateChecker()) {
            this._updateChecker = updateChecker
        }
        return this
    }

    private fun isInitUpdateChecker(): Boolean {
        return this::_updateChecker.isInitialized
    }

    fun getUpdateChecker(): IUpdateChecker {
        if (!isInitUpdateChecker()) {
            setUpdateChecker(DefaultUpdateChecker())
        }
        return _updateChecker
    }

    fun setUpdateListener(updateListener: IUpdateListener): XUpdate {
        if (!isInitUpdateListener()) {
            this._updateListener = updateListener
        }
        return this
    }

    private fun isInitUpdateListener(): Boolean {
        return this::_updateListener.isInitialized
    }

    fun getUpdateListener(): IUpdateListener {
        if (!isInitUpdateListener()) {
            setUpdateListener(DefaultUpdateListener())
        }
        return _updateListener
    }

    fun setUpdateConfig(updateConfig: IUpdateConfig): XUpdate {
        if (!isInitUpdateConfig()) {
            this._updateConfig = updateConfig
        }
        return this
    }

    private fun isInitUpdateConfig(): Boolean {
        return this::_updateConfig.isInitialized
    }

    fun getUpdateConfig(): IUpdateConfig {
        if (!isInitUpdateConfig()) {
            setUpdateConfig(DefaultUpdateConfig())
        }
        return _updateConfig
    }

    fun setUpdateDownloader(updateLoader: IUpdateDownloader): XUpdate {
        if (!isInitUpdateDownloader()) {
            this._updateDownloader = updateLoader
        }
        return this
    }

    private fun isInitUpdateDownloader(): Boolean {
        return this::_updateDownloader.isInitialized
    }

    fun getUpdateDownloader(): IUpdateDownloader {
        if (!isInitUpdateDownloader()) {
            setUpdateDownloader(DefaultUpdateDownloader())
        }
        return _updateDownloader
    }

    fun setUpdateInstaller(updateInstaller: IUpdateInstaller): XUpdate {
        if (!isInitUpdateInstaller()) {
            this._updateInstaller = updateInstaller
        }
        return this
    }

    private fun isInitUpdateInstaller(): Boolean {
        return this::_updateInstaller.isInitialized
    }

    fun getUpdateInstaller(): IUpdateInstaller {
        if (!isInitUpdateInstaller()) {
            setUpdateInstaller(DefaultUpdateInstaller())
        }
        return _updateInstaller
    }

    fun setDownloading(isDownloading: Boolean){
        _isDownloading = isDownloading
    }

    fun isDownloading() : Boolean{
        return _isDownloading
    }

}